﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace Water.Model
{
    public class SmallValveOperatePlan
    {
        public string MRNO { get; set; }
        public DateTime FillDt { get; set; }
        public DateTime StopDt { get; set; }
        public string Address { get; set; }
        public DateTime WorkDt { get; set; }
        public string WorkNr { get; set; }
        public string WorkNo { get; set; }
        public string ValveID { get; set; }
        public DateTime StopYdDt { get; set; }
        public DateTime StartYdDt { get; set; }
        public string BeforeOperZb { get; set; }
        public string PingGyx { get; set; }
        public string AfterOper { get; set; }
        public string OperImage { get; set; }
        public string OperEmp { get; set; }
        public DateTime CreateDt { get; set; }

        /// <summary>
        /// 新增小阀门操作
        /// </summary>
        /// <param name="svop"></param>
        /// <returns></returns>
        public int AddSmallValveOperatePlan(SmallValveOperatePlan svop)
        {
            string sql = "Execute Proc_SmallValveOperatePlan '" + svop.MRNO + "','" + svop.FillDt + "','" + svop.StopDt + "','"
                + svop.Address + "','" + svop.WorkDt + "','" + svop.WorkNr + "','" + svop.WorkNo + "','" + svop.ValveID + "','"
                + svop.StopYdDt + "','" + svop.StartYdDt + "','" + svop.BeforeOperZb + "','" + svop.PingGyx + "','" + svop.AfterOper + "','"
                + svop.OperImage + "','" + svop.OperEmp + "'";
            sqldb db = new sqldb();
            //WriteSqlTotxt.writesql(sql);
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }

        /// <summary>
        /// 删除小阀门操作
        /// </summary>
        /// <param name="mrno"></param>
        /// <returns></returns>
        public int DeleteSmallValveOperatePlan(string mrno,string vid)
        {
            string sql = "Execute Proc_DeleteSmallValveOperatePlan '" + mrno + "','" + vid + "'";
            sqldb db = new sqldb();
            //WriteSqlTotxt.writesql(sql);
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }
    }

    public class SmallValveOperatePlanQuery : SmallValveOperatePlan
    {
        public virtual DateTime CloseDt { get; set; }
        public virtual DateTime StartDt { get; set; }
        public virtual int ZhuanCount { get; set; }
        public virtual string StartDirection { get; set; }

        public SmallValveOperatePlanQuery GetSmallValveOperatePlanQuery(string mrno)
        {
            string sql = "select a.MRNO,a.FillDt,a.StopDt,a.Address,a.WorkDt,a.WorkNr,a.WorkNo,a.ValveID,a.StopYdDt,"
                        + "a.StartYdDt,a.BeforeOperZb,a.PingGyx,"
                        + "a.AfterOper,a.OperImage,b.CloseDt,b.StartDt,b.ZhuanCount,b.StartDirection,a.OperEmp,a.CreateDt "
                        + "from Tbl_SmallValveOperatePlan a inner join Tbt_SmallValveOperate b on a.MRNO = b.MRNO where a.MRNO = '" + mrno + "'";
            sqldb db = new sqldb();
            //WriteSqlTotxt.writesql(sql);
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            SmallValveOperatePlanQuery svopq = new SmallValveOperatePlanQuery();
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                svopq.MRNO = dr["MRNO"].ToString();
                svopq.FillDt = Convert.ToDateTime(dr["FillDt"]);
                svopq.StopDt = Convert.ToDateTime(dr["StopDt"]);
                svopq.Address = dr["Address"].ToString();
                svopq.WorkDt = Convert.ToDateTime(dr["WorkDt"]);
                svopq.WorkNr = dr["WorkNr"].ToString();
                svopq.WorkNo = dr["WorkNo"].ToString();
                svopq.ValveID = dr["ValveID"].ToString();
                svopq.StopYdDt = Convert.ToDateTime(dr["StopYdDt"]);
                svopq.CloseDt = Convert.ToDateTime(dr["CloseDt"]);
                svopq.StartYdDt = Convert.ToDateTime(dr["StartYdDt"]);
                svopq.StartDt = Convert.ToDateTime(dr["StartDt"]);
                svopq.ZhuanCount = Convert.ToInt32(dr["ZhuanCount"]);
                svopq.StartDirection = dr["StartDirection"].ToString();
                svopq.BeforeOperZb = dr["BeforeOperZb"].ToString();
                svopq.PingGyx = dr["PingGyx"].ToString();
                svopq.AfterOper = dr["AfterOper"].ToString();
                svopq.OperImage = dr["OperImage"].ToString();
                svopq.OperEmp = dr["OperEmp"].ToString();
                svopq.CreateDt = Convert.ToDateTime(dr["CreateDt"]);

            }
            return svopq;
        }
    }
}
